package com.library.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.library.bean.Book;
import com.library.bean.Page;
import com.library.service.BookService;
import com.library.service.impl.BookServiceImpl;
@WebServlet("/getBook.do")
public class GetBookServlet extends HttpServlet{
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		int pageNum;
		int pageSize;
		String bookId;
		String bookNo;
		String bookName;
		String bookWriter;
		double minBookPrice;
		double maxBookPrice;
		String bookType;
		try {
			pageNum = Integer.parseInt(req.getParameter("pageNum"));
		} catch (Exception e) {
			pageNum = 1;
		}
		try {
			pageSize = Integer.parseInt(req.getParameter("pageSize"));
		} catch (Exception e) {
			pageSize = 5;
		}
		try {
			bookId = req.getParameter("bookId");
		} catch (Exception e) {
			bookId = "";
		}
		try {
			bookNo = req.getParameter("bookNo");
		} catch (Exception e) {
			bookNo = "";
		}
		try {
			bookName = req.getParameter("bookName");
		} catch (Exception e) {
			bookName = "";
		}
		try {
			bookWriter = req.getParameter("bookWriter");
		} catch (Exception e) {
			bookWriter = "";
		}
		System.out.println(bookWriter);
		try {
			maxBookPrice = Double.parseDouble(req.getParameter("maxBookPrice"));
		} catch (Exception e) {
			maxBookPrice = 100;
		}
		try {
			minBookPrice = Double.parseDouble(req.getParameter("minBookPrice"));
		} catch (Exception e) {
			minBookPrice = 0;
		}
		try {
			bookType = req.getParameter("bookType");
		} catch (Exception e) {
			bookType = "";
		}
		BookService bookService = new BookServiceImpl();
		Page<Book> page = bookService.getBookAll(pageNum, pageSize, bookId, bookNo, bookName, bookWriter, minBookPrice, maxBookPrice, bookType);
		ObjectMapper om = new ObjectMapper();
		resp.setContentType("application/json;charset=UTF-8");
		String bookJson = om.writeValueAsString(page);
		resp.getWriter().write(bookJson);
	}
}
